package com.henu.rescue.controller;

import com.henu.rescue.entity.VO.AccidentMapVO;
import com.henu.rescue.entity.VO.StationComplainCount;
import com.henu.rescue.entity.VO.StationMapVO;
import com.henu.rescue.service.AccidentService;
import com.henu.rescue.service.StaService;
import com.henu.rescue.utils.Result;
import com.henu.rescue.utils.ResultEnums;
import com.henu.rescue.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("map/")
@Api("地图相关信息")
@Slf4j
public class MapController {

    @Autowired
    private StaService staService;

    @Autowired
    private AccidentService accidentService;

    @PostMapping("StaVO")
    @ApiOperation("获取每个服务点地图展示信息")
    public Result getStaVO(){
        List<StationMapVO> stationMapVOS = staService.selectStationMap();
        //将坐标信息拆封成单独的经纬度
        for (int i = 0; i < stationMapVOS.size(); i++) {
            StationMapVO stationMapVO = stationMapVOS.get(i);
            String locationString = stationMapVO.getStationLocation();
            double[] locationDouble = StringUtils.parseTwoDoubles(locationString);
            stationMapVO.setLng(locationDouble[0]);
            stationMapVO.setLat(locationDouble[1]);
        }
        return new Result(ResultEnums.SUCCESS,stationMapVOS);
    }

    @PostMapping("AccidentVO")
    @ApiOperation("获取每个事故的地图展示信息")
    public Result AccidentVO(){
        List<AccidentMapVO> accidentMapVOS = accidentService.selectAccidentMapVO();
        //将坐标信息拆封成单独的经纬度
        for (int i = 0; i < accidentMapVOS.size(); i++) {
            AccidentMapVO accidentMapVO = accidentMapVOS.get(i);
            String locationString = accidentMapVO.getLocation();
            double[] doubles = StringUtils.parseTwoDoubles(locationString);
            accidentMapVO.setLng(doubles[0]);
            accidentMapVO.setLat(doubles[1]);
            Date happenTime = accidentMapVO.getHappenTime();
            Date endTime = accidentMapVO.getEndTime();
            // 定义所需的日期格式
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            try {
                // 格式化Date对象为所需的时间日期字符串
                String formattedDate = dateFormat.format(happenTime);
                String format = dateFormat.format(endTime);
                accidentMapVO.setHappenTimeString(formattedDate);
                accidentMapVO.setEndTimeString(format);
            } catch (Exception e) {
                System.out.println("An exception occurred: " + e.getMessage());
            }

        }
        return new Result(ResultEnums.SUCCESS,accidentMapVOS);
    }
}
